﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Common.Lib.Assembly;
using NHibernate.Cfg;

namespace NHibernate.Util
{
    public abstract class ConfigurationBuilderBase
    {

        public Configuration BuildConfiguration()
        {
            Dictionary<string, string> properties = new Dictionary<string, string>();
            properties.Add(NHibernate.Cfg.Environment.ShowSql, "true");
            properties.Add(NHibernate.Cfg.Environment.ReleaseConnections, "on_close");
            properties.Add(NHibernate.Cfg.Environment.ConnectionDriver, GetConnectionDriver());
            properties.Add(NHibernate.Cfg.Environment.Dialect, GetDialect());
            properties.Add(NHibernate.Cfg.Environment.ConnectionProvider, GetConnectionProvider());
            properties.Add(NHibernate.Cfg.Environment.ConnectionString, GetConnectionString());

            Dictionary<string, string> customizedProperties = GetCustomizedProperties();
            properties = ConfigurationUtil.CombineProperties(properties, customizedProperties);
            
            Dictionary<string, IEnumerable<string>> dict = GetResoruces();
            Configuration cfg = ConfigurationUtil.BuildConfiguration(properties, dict);

            return cfg;
        }

        protected abstract string GetConnectionDriver();
        protected abstract string GetDialect();
        protected abstract string GetConnectionProvider();
        protected abstract string GetConnectionString();
        //by this method the properties that are defined in the "BuildPart" methods, can be overridden.
        protected abstract Dictionary<string, string> GetCustomizedProperties();
        protected abstract Dictionary<string, IEnumerable<string>> GetResoruces();
    }
}
